package Adapters;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import entities.Hero;

public class ImageAdapter extends BaseAdapter {
	
	 private Context mContext;
	 private static int jumpPos = 0;
	    public ImageAdapter(Context c) {
	        mContext = c;
	    }



	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    
	    Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException {
	        return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);

	    }
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	
	        ImageView imageView;
	        
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	            imageView = new ImageView(mContext);
	            imageView.setLayoutParams(new GridView.LayoutParams(244, 150));
	            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	            imageView.setPadding(8, 8, 8, 8);
	            
	        } else {
	            imageView = (ImageView) convertView;
	            
	        }
            
            try {
            	AssetManager am = mContext.getAssets();
				InputStream in = am.open(Hero.xmlFileName);
				Hero h = new Hero(in);
				while(h.getHeroImageUrl(position+jumpPos)== null){
					jumpPos++;
				}
				DownloadImageTask d = new DownloadImageTask((ImageView) imageView);
				d.execute(h.getHeroImageUrl(position+jumpPos));
				imageView = d.getBmImage();
				
            } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return imageView;

	    }



		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Hero.count;
		}

	    // references to our images


	  
}





